package com.tuanzi.loan.business.service.family.impl;

import com.tuanzi.loan.business.entity.family.ClassificationInfo;
import com.tuanzi.loan.business.repository.family.ClassificationRepository;
import com.tuanzi.loan.business.repository.family.ClassificationRepositoryCustom;
import com.tuanzi.loan.business.service.family.ClassificationService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.ClassificationInfoSearchRequest;
import com.tuanzi.loan.vo.response.family.ClassificationInfoListResponse;
import com.tuanzi.loan.vo.view.family.ClassificationView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class ClassificationServiceImpl implements ClassificationService {

    @Autowired
    private ClassificationRepositoryCustom classificationRepository;

    @Autowired
    private ClassificationRepository jpaRepository;

    @Override
    public Page<ClassificationInfoListResponse> list(PageSearcher<ClassificationInfoSearchRequest> pageSearcher) {
        return classificationRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public ClassificationView getClassificationById(long id) {
        ClassificationInfo info = jpaRepository.getOne(id);
        ClassificationView viewInfo = new ClassificationView();
        try {
            BeanUtils.copyProperties(info,viewInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return viewInfo;
    }

    @Override
    public Long getLastId() {
        return classificationRepository.getLastId();
    }

    @Override
    public Integer getBiggestSort() {
        return classificationRepository.getBiggestSort();
    }

    @Override
    public void saveClassification(ClassificationInfoSearchRequest request) throws InvocationTargetException, IllegalAccessException {
        validRequest(request);
        ClassificationInfo classificationInfo = new ClassificationInfo();
        BeanUtils.copyProperties(request,classificationInfo);
        ClassificationInfo classinfoFromDb = jpaRepository.findOne(classificationInfo.getId());
        if(classinfoFromDb == null){
            classificationInfo.setCreatetime(new Date());
        }else{
            classificationInfo.setCreatetime(classinfoFromDb.getCreatetime());
            classificationInfo.setUpdatetime(new Date());
        }
        jpaRepository.save(classificationInfo);

    }

    @Override
    public void deleteClassifById(long id) {
        jpaRepository.delete(id);
    }

    @Override
    public List<ClassificationInfo> loadClassifications() {
        List<ClassificationInfo> all = jpaRepository.findAll();
        return all;
    }

    public void validRequest(ClassificationInfoSearchRequest request){
        if(StringUtils.isEmpty(request.getClassif_name())){
            throw new IllegalArgumentException("参数 分类名称 为空");
        }
        if(request.getSort() == null){
            throw new IllegalArgumentException("参数 分类排序 为空");
        }
        if(request.getStatus() == null){
            throw new IllegalArgumentException("参数 分类状态 为空");
        }
    }

    @Override
    public ClassificationRepository getJpaRepository() {
        return jpaRepository;
    }
}
